﻿$(window).ready(function () {
    var orderid = $("#orderid").val();
    var orderRef = $("#orderref").val();
    var customerid = $("#customerid").val();
    //load order summary
    $.ajax({
        url: '/orderview/LoadOrderSummary',
        data: { orderid: orderid, customerid: customerid, orderRef: orderRef },
        cache: false,
        success: function (data) {
            $(".order-summary").html(data);
        }, error: function () {
            $(".order-summary").html("Load failed");
        }
    });
    $.ajax({
        url: '/orderview/LoadOrderItems',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $("#lstorder-items").html(data);
            $(".tbl-orderitemdetail tbody tr").click(function () {
                $(this).addClass("row-selected");
                $(".tbl-orderitemdetail tbody tr").not(this).removeClass('row-selected');
            });
            InitAjaxTable($(".tbl-orderitemdetail"));
            InitDatetimePicker();
        }, error: function () {
            $("#lstorder-items").html("Load failed");
        }
    });
    //load tasks
    $.ajax({
        url: '/orderview/LoadTasks',
        data: { orderid: orderid, orderRef: orderRef },
        cache: false,
        success: function (data) {
            $(".tasks").html(data);
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $(".tasks").html("Load failed");
        }
    });
    $('a[href="#RDM"]').click(function () {

        if ($('.rdm').html() == "") {
            $('.rdm').html(' <div class="ajax-loading"></div>');
            //load rdm
            $.ajax({
                url: '/orderview/loadrdm',
                data: { orderid: orderid },
                success: function (data) {
                    $(".rdm").html(data);
                    var $table = $('.fixhead .table-fix');
                    $table.floatThead({
                        scrollContainer: function ($table) {
                            return $table.closest('.fixhead');
                        }
                    });
                },
                error: function () {
                    $(".rdm").html("Load failed");
                }
            });
        }
    });
    $('a[href="#Email"]').click(function () {
        if ($('.email-history').html() == "") {
            $('.email-history').html(' <div class="ajax-loading"></div>');    //load email history
            $.ajax({
                url: '/orderview/loademailhistory',
                data: { orderid: orderid },
                cache: false,
                success: function (data) {
                    $(".email-history").html(data);
                    var $table = $('.fixhead .table-fix');
                    $table.floatThead({
                        scrollContainer: function ($table) {
                            return $table.closest('.fixhead');
                        }
                    });
                }, error: function () {
                    $(".email-history").html("Load failed");
                }
            });
        }
    });
    $('a[href="#Supplier"]').click(function () {

        if ($('.supplier').html() == "") {
            $('.supplier').html(' <div class="ajax-loading"></div>');
            //load supplier
            $.ajax({
                url: '/orderview/loadsupplier',
                data: { orderid: orderid },
                cache: false,
                success: function (data) {
                    $(".supplier").html(data);
                    $(".tbl-selected tbody tr").click(function () {
                        $(this).addClass("row-selected");
                        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
                    });
                    var $table = $('.fixhead .table-fix');
                    $table.floatThead({
                        scrollContainer: function ($table) {
                            return $table.closest('.fixhead');
                        }
                    });
                },
                error: function () {
                    $(".supplier").html("Load failed");
                }
            });
        }
    });
    $('a[href="#LogHistory"]').click(function () {
        if ($('.loghistory').html() == "") {
            $('.loghistory').html(' <div class="ajax-loading"></div>');
            //load log history
            $.ajax({
                url: '/orderview/loadloghistory',
                data: { orderid: orderid },
                cache: false,
                success: function (data) {
                    $(".loghistory").html(data);
                    $(".tbl-selected tbody tr").click(function () {
                        $(this).addClass("row-selected");
                        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
                    });
                    var $table = $('.fixhead .table-fix');
                    $table.floatThead({
                        scrollContainer: function ($table) {
                            return $table.closest('.fixhead');
                        }
                    });
                },
                error: function () {
                    $(".supplier").html("Load failed");
                }
            });
        }
    });

    $('a[href="#MessageToDriver"]').click(function () {
        if ($('#MessageToDriver').html() == "") {
            $('#MessageToDriver').html(' <div class="ajax-loading"></div>');
            $.ajax({
                url: '/orderview/MessageToDriver',
                data: { orderid: orderid },
                cache: false,
                success: function(data) {
                    $("#MessageToDriver").html(data);
                },
                error: function() {
                    $("#MessageToDriver").html("Load failed");
                }
            });
        }
    });
});

function loadorderitemagain(orderid) {
    App.blockUI($("#lstorder-items"));
    $.ajax({
        url: '/orderview/LoadOrderItems',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $("#lstorder-items").html(data);
            $(".tbl-orderitemdetail tbody tr").click(function () {
                $(this).addClass("row-selected");
                $(".tbl-orderitemdetail tbody tr").not(this).removeClass('row-selected');
            });
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
            App.unblockUI($("#lstorder-items"));
        }, error: function () {
            $("#lstorder-items").html("Load failed");
            App.unblockUI($("#lstorder-items"));
        }
    });
}

/* backup
$(window).load(function () {
    var orderid = $("#orderid").val();
    //load order summary
    $.ajax({
        url: '/orderview/LoadOrderSummary',
        data: {orderid : orderid},
        cache: false,
        success: function (data) {
            $(".order-summary").html(data);
        }, error: function () {
            $(".order-summary").html("Load failed");
        }
    });
    $.ajax({
        url: '/orderview/LoadOrderItems',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $("#lstorder-items").html(data);
            $(".tbl-orderitemdetail tbody tr").click(function () {
                $(this).addClass("row-selected");
                $(".tbl-orderitemdetail tbody tr").not(this).removeClass('row-selected');
            });
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $("#lstorder-items").html("Load failed");
        }
    });
    //load tasks
    $.ajax({
        url: '/orderview/LoadTasks',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $(".tasks").html(data);
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $(".tasks").html("Load failed");
        }
    });
    //load email history
    $.ajax({
        url: '/orderview/loademailhistory',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $(".email-history").html(data);
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $(".email-history").html("Load failed");
        }
    });
    
    //load supplier
    $.ajax({
        url: '/orderview/loadsupplier',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $(".supplier").html(data);
            $(".tbl-selected tbody tr").click(function () {
                $(this).addClass("row-selected");
                $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
            });
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $(".supplier").html("Load failed");
        }
    });
    
    //load supplier
    $.ajax({
        url: '/orderview/loadloghistory',
        data: { orderid: orderid },
        cache: false,
        success: function (data) {
            $(".loghistory").html(data);
            $(".tbl-selected tbody tr").click(function () {
                $(this).addClass("row-selected");
                $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
            });
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function () {
            $(".supplier").html("Load failed");
        }
    });
    
    

    //load rdm
    $.ajax({
        url: '/orderview/loadrdm',
        data: { orderid: orderid },
        success: function (data) {
            $(".rdm").html(data);
            var $table = $('.fixhead .table-fix');
            $table.floatThead({
                scrollContainer: function ($table) {
                    return $table.closest('.fixhead');
                }
            });
        }, error: function() {
            $(".rdm").html("Load failed");
        }
    });
});


*/